package com.stlm2.dems.user.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.user.entity.IsInit;
import com.stlm2.dems.user.entity.IsValid;
import com.stlm2.dems.entity.User;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.user.entity.Role;
import com.stlm2.dems.user.dao.RoleDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service("roleService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class RoleService extends BaseServiceImpl<Role> {

	@Resource
	private RoleDao roleDao;
	@Resource
	private UserDao userDao;

	@Override
	protected BaseDao <Role> getDao() {
		return roleDao;
	}
	/*zlq
     *添加角色
     * @param  name :角色名字
     * @param  label :标签名称
     * @param  isInit :是否允许后台创建用户时直接使用
      */
	public String addRole(ModelMap model,String token,String name,String label,String isInit){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "请登录", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {

			return CodeContent.getModelByCode(-1, "请登录", model);
		}
		if (StringUtils.isEmpty(name)) {
			return CodeContent.getModelByCode(0, "角色名不能为空", model);
		}
		if (StringUtils.isEmpty(label)) {
			return CodeContent.getModelByCode(0, "标签名不能为空", model);
		}
		if (!NumberUtils.isNumber(isInit)) {
			return CodeContent.getModelByCode(0, "请选择是否可直接创建", model);
		}
		Role role=roleDao.getByName(name.trim());
		if(role!=null){
			return CodeContent.getModelByCode(0, "角色名已存在", model);
		}
		role=roleDao.getByLabel(label.trim());
		if(role!=null){
			return CodeContent.getModelByCode(0, "标签名已存在", model);
		}
		Role role1=new Role();
		role1.setName(name.trim());
		role1.setLabel(label.trim());
		role1.setIsValid(IsValid.Normal);
		role1.setIsInitValue(Integer.parseInt(isInit));
		roleDao.add(role1);
		return CodeContent.getModelByCode(1,"",model);
	}


	/*
*查询有效的的角色
* @param  page ：页码
* @param  token
 */
	public String getNormalRole(ModelMap map,String token,String page){
		PageView pageview =new  PageView();
		int pageNo = 1;
		int pageSize=20;
		if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
			pageNo = NumberUtils.toInt(page);
		}
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "请登录", map);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "请登录", map);
		}
		Role role=new Role();
		role.setIsValid(IsValid.Normal);
		pageview=roleDao.queryPageView(pageNo, pageSize,role);
		map.put("datas",convertList(pageview.getRecords()));
		map.put("recordsTotal", pageview.getRowCount());
		map.put("totalPage", pageview.getPageCount());
		map.put("pageNow", pageview.getPageNow());
		map.put("code",1);
		map.put("msg","success");
		return new JSONObject(map).toString();
	}
	public List<ModelMap> convertList(List<Role> roleList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (roleList != null && roleList.size() > 0) {
			for (Role role : roleList) {
				ModelMap map = buildModelList(role);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelList(Role role) {
		ModelMap model = new ModelMap();
		model.put("id",role.getId());
		model.put("name",role.getName());
		model.put("label",role.getLabel());
		model.put("isInitValue",role.getIsInitValue());
		model.put("isInit",role.getIsInit().toString());
		model.put("isValid",role.getIsValid().toString());
		model.put("isValidValue",role.getIsValidValue());
		return model;
	}


	/*
	*查询有效的可初始化的角色
	 */
	public ModelMap getInitValidRole(ModelMap map,IsInit isInit){
		Role role=new Role();
		role.setIsValid(IsValid.Normal);
//		role.setIsInit(IsInit.Allow);
		role.setIsInit(isInit);
		List<Role> roleList=roleDao.queryList(-1,-1,role);
		map.put("datas",convertList(roleList));
		map.put("code",1);
		map.put("msg","success");
		return map;
	}

}